clear all 
capture log close

global data "/Users/ceweber/Dropbox/JPAM_TIV_code/original_data/"
global dta "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_files/"
global dataclean "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_output/"
global output "/Users/ceweber/Dropbox/JPAM_TIV_code/output/"

set more off
cd "$dataclean"

local count=0
set more off
 forvalues l =227(1)812{
capture noisily use multiplier_tax_`l'.dta, clear
summ location
local mean=r(mean)
if `mean'!=`l' continue
local count=`count'+1
di `count'


	qui gen maxsum_multfinal= sum_multfinal1 
	qui gen maxsum_multfinal2= sum_multfinal1 
	qui sum taxmultfinal if group_final==1
	qui gen maxtaxmultfinal=round(r(mean), .001)
	qui gen maxtaxmultfinal2=round(r(mean), .001) 
	summ group_final
	local max=r(max)
	forvalues r = 2(1)`max' {
		qui sum taxmultfinal if group_final==`r'
		qui replace maxtaxmultfinal2=round(r(mean), .001) if (sum_multfinal`r'>maxsum_multfinal2&sum_multfinal`r'<.)|maxsum_multfinal2==.|maxtaxmultfinal==0
		qui replace maxsum_multfinal2=sum_multfinal`r' if (sum_multfinal`r'>maxsum_multfinal2&sum_multfinal`r'<.)|maxsum_multfinal2==.|maxtaxmultfinal==0
		qui sum taxmultfinal if group_final==`r'
		qui replace maxtaxmultfinal=round(r(mean), .001) if (sum_multfinal`r'>maxsum_multfinal&sum_multfinal`r'<.)|maxsum_multfinal==.
		qui replace maxsum_multfinal=sum_multfinal`r' if (sum_multfinal`r'>maxsum_multfinal&sum_multfinal`r'<.)|maxsum_multfinal==.
		}
		
*now that "chosen" multiplier, calculate roundedness: 
	 foreach x in maxtaxmultfinal2 {
	qui gen price0 = price*`x'
	qui gen round_price0 = round(price0, .25)
	qui gen check = abs(price0 - round_price0)
	qui gen round = 1 if check<.01
	qui replace round = 0 if round==.
	qui rename check dist
	
	qui gen d_cut=.
	
	//calculate discount off last known round price, make discounts count as round 
	qui gen price1 = price*`x'
	qui gen last_round = price1 if round==1
	sort inventoryid date			
	bysort inventoryid: carryforward last_round, replace //we now have the last observed round price for each ob
	qui gen discount = price1/last_round
	
	forvalues c = .05(.05)1{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
		
	foreach c in .33 .67{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
				
	drop discount		

	gen d_cut_b4 = .
	//make intro discounts count as round 
	qui gen first_round = price1 if round==1
	gsort inventoryid -date			
	bysort inventoryid: carryforward first_round, replace			
	qui replace first_round = . if last_round!=.
	qui gen discount = price1/first_round
	*qui replace discount = round(discount, .01)
	forvalues c = .05(.05)1{			
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99			
	}		
	foreach c in .33 .67{
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99
		}
	drop discount last_round first_round price0	
					
	//replace round if it's a discount from a round price
	sort inventoryid date
	qui replace round = 1 if round==0&d_cut==1&round[_n-1]==1
	gsort inventoryid -date
	qui replace round = 1 if round==0&d_cut_b4==1&round[_n-1]==1


	qui egen max_round`x' = mean(round), by(inventoryid)
	rename round daily_round`x'
	drop round_price0 dist d_cut price1 d_cut_b4
	}
	
	//how round is each tax mult?  wider range of roundness for plots	
foreach x in maxtaxmultfinal2 {
	qui gen price0 = price_cleaned*`x'
	qui gen round_price0 = round(price0, .25)
	qui gen check = abs(price0 - round_price0)
	qui gen round = 1 if check<.02
	qui replace round = 0 if round==.
	qui rename check dist
	
	qui gen d_cut=.
	
	//calculate discount off last known round price, make discounts count as round 
	qui gen price1 = price_cleaned*`x'
	qui gen last_round = price1 if round==1
	sort inventoryid date			
	bysort inventoryid: carryforward last_round, replace //we now have the last observed round price for each ob
	qui gen discount = price1/last_round
	*qui replace discount = round(discount, .01)
	
	forvalues c = .05(.05)1{
		qui replace d_cut = 1 if abs(discount-`c')<.01&discount<.99
		}	
		
	foreach c in .33 .67{
		qui replace d_cut = 1 if abs(discount-`c')<.01&discount<.99
		}	
				
	drop discount		

	gen d_cut_b4 = .
	//make intro discounts count as round 
	qui gen first_round = price1 if round==1
	gsort inventoryid -date			
	bysort inventoryid: carryforward first_round, replace			
	qui replace first_round = . if last_round!=.
	qui gen discount = price1/first_round
	*qui replace discount = round(discount, .01)
	forvalues c = .05(.05)1{			
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.01&discount<.99			
	}		
	foreach c in .33 .67{
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.01&discount<.99
		}
	drop discount last_round first_round price0	
					
	//replace round if it's a discount from a round price
	sort inventoryid date
	qui replace round = 1 if round==0&d_cut==1&round[_n-1]==1
	gsort inventoryid -date
	qui replace round = 1 if round==0&d_cut_b4==1&round[_n-1]==1


	*qui egen max_round2 = mean(round), by(inventoryid)
	rename round daily_roundw`x'
	drop round_price0 dist d_cut price1 d_cut_b4 
	}	
	

		
 ***now plot out the final results: 
 	summ group_final
	local max=r(max)
	forvalues r = 1(1)`max' {
	qui replace sum_multfinal`r'=. if sum_multfinal`r'!=maxsum_multfinal
	}
		summ group_final
	local max=r(max)
	forvalues r = 1(1)`max' {
	summ sum_multfinal`r'
	local mean=r(mean)
	if `mean'==. {
	drop sum_multfinal`r'
	}
	}
	
 
	
	drop round_weekly
	qui egen round_weekly=mean(daily_roundwmaxtaxmultfinal2), by(weekly)
	label var  round_weekly "Tax Round"
	
	
	
	/* cd "$plots"
	sort weekly
	twoway (line sum_multfinal* weekly)  || (line round_weekly  weekly, yaxis(2)) , ///  
	xline(2886, lcolor(black) lpattern(dash))  title("Tax Mult, All Transactions: `l'") ///
	ytitle("Multiplier Count", margin(medsmall))  xtitle("Week") ylabel(0(.2)1, axis(2))  ///
	graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
	legend(region(lcolor(none))) xlabel(#6, valuelabel angle(forty_five))	
	qui graph export maxtax_mult_`l'.png, as(png) replace */
	
	egen group_i=group(inventoryid)
xtset group_i date	 

foreach x in taxmultboth taxmultbothdiv taxmultexcise {
	gen pricediff_none`x'=`x'*price-L2.price if date==20271
	replace pricediff_none`x'=`x'*price-L3.price if date==20271&pricediff_none`x'==.
	replace pricediff_none`x'=`x'*price-L4.price if date==20271&pricediff_none`x'==.
	replace pricediff_none`x'=`x'*price-L5.price if date==20271&pricediff_none`x'==.
	gen pricechange_none`x'=abs(pricediff_none`x')>.01
	replace pricechange_none`x'=. if pricediff_none`x'==.
	egen tmpx=mean( pricechange_none`x') 
	replace pricechange_none`x'=tmpx
	drop tmpx
	
	gen pricediff_sales`x'=`x'*price-L2.taxmultsales*L2.price if date==20271
	replace pricediff_sales`x'=`x'*price-L3.taxmultsales*L3.price if date==20271&pricediff_none`x'==.
	replace pricediff_sales`x'=`x'*price-L4.taxmultsales*L4.price if date==20271&pricediff_none`x'==.
	replace pricediff_sales`x'=`x'*price-L5.taxmultsales*L5.price if date==20271&pricediff_none`x'==.
	gen pricechange_sales`x'=abs(pricediff_sales`x')>.01
	replace pricechange_sales`x'=. if pricediff_sales`x'==.
	egen tmpx=mean( pricechange_sales`x') 
	replace pricechange_sales`x'=tmpx
	drop tmpx
}


gen leastchange=pricechange_salestaxmultboth
gen taxmultleastchange="pn" if leastchange<.
foreach x in nonetaxmultboth nonetaxmultbothdiv nonetaxmultexcise salestaxmultbothdiv salestaxmultexcise {
replace taxmultleastchange="`x'" if pricechange_`x'<leastchange&leastchange<.&pricechange_`x'<.
replace leastchange = pricechange_`x' if pricechange_`x'<leastchange&leastchange<.&pricechange_`x'
}

egen taglw=tag(location weekly) if maxtaxmultfinal<.
keep if taglw==1
keep location weekly maxtaxmultfinal* round_weekly taxmultsales taxmultboth taxmultexcise taxmultbothdiv taxmultleastchange leastchange pricechange*
save multiplier_maxtax_`count'.dta, replace	
		
 
 	}
	
 	
set more off
use multiplier_maxtax_1.dta, clear
forvalues l =2(1)111{
append using multiplier_maxtax_`l'.dta
}
save multiplier_maxtax_all.dta, replace

use  multiplier_maxtax_all.dta, clear
gen run=weekly-2886
gen tmpx=maxtaxmultfinal if abs(run)<=8&run<0
egen maxtaxpre=max(tmpx), by(location)
drop tmpx
gen tmpx=maxtaxmultfinal if abs(run)<=8&run>=0
egen maxtaxpost=max(tmpx), by(location)
drop tmpx
gen tmpx=round_weekly if abs(run)<=8&run<0
egen roundpre=mean(tmpx), by(location)
drop tmpx
gen tmpx=round_weekly  if abs(run)<=8&run>=0
egen roundpost=mean(tmpx), by(location)
drop tmpx
save multiplier_maxtax_all.dta, replace
